package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ShoppingMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;


@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShoppingMapper shoppingMapper;

    @Override
    public String reg(User user) {
        User uu=userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", user.getUsername())
        );
        if(uu!=null){
            return "失败";
        }
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes())).setStatus(true);
        user.setImages("1.jpg");
        userMapper.insert(user);
        return "成功";
    }

    @Override
    public User login(User user) {
        String password=user.getPassword();
        String md5Pwd= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pwd);
        User userDB=userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", user.getUsername())
                .eq("password", user.getPassword())
        );
        if(userDB==null){
            return null;
        }
        String token=UUID.randomUUID().toString().replace("-", "");
        userDB.setToken(token);
        return userDB;
    }

    @Override
    public PageResult findUserListByPage(PageResult pageResult) {
        Page<User> page=new Page(pageResult.getPageNum(),pageResult.getPageSize());
        page=userMapper.selectPage(page, new QueryWrapper<User>()
                .like(StringUtils.hasLength(pageResult.getQuery()),"username",pageResult.getQuery())
        );
        long total=page.getTotal();
        List<User> userList=page.getRecords();
        pageResult.setTotal(total).setRows(userList);
        return pageResult;
    }

    @Override
    public void updateStatus(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional//Spring默认提供的事务控制的注解
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public Object findUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//Spring默认提供的事务控制的注解
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
        shoppingMapper.deleteShopping(id);
    }

    @Override
    public void addUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes())).setStatus(true);
        user.setImages("1.jpg");
        userMapper.insert(user);
    }
}
